package com.sdkx.app.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.SectionIndexer;
import android.widget.TextView;

import com.sdkx.app.R;
import com.sdkx.app.model.Mark;

import java.util.List;


public class MarkListAdapter extends BaseAdapter implements SectionIndexer {
    private Context context;
    private List<Mark> list;

    public MarkListAdapter(Context context, List<Mark> list) {
        this.context = context;
        this.list = list;

    }

    private int selectpos=-1;
    @Override
    public int getCount() {
        return list.size();
    }

    public void setSelectedPosition(int position) {
        selectpos = position;
    }

    @Override
    public Object getItem(int i) {
        return list.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup viewGroup) {
        final ViewHolder holder;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.wplist_item, null);
            holder = new ViewHolder(convertView);
            holder.submenu.setVisibility(View.GONE);
//
//            holder.mainmenu.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    ViewHolder tempholder;
//
//                    thisholder = holder;
//                    if (holder.submenu.getVisibility() == View.VISIBLE)
//                        holder.submenu.setVisibility(View.GONE);
////                    else
////                        holder.submenu.setVisibility(View.VISIBLE);
//
//                }
//            });
            holder.button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            holder.button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            holder.button3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
            holder.tvWpname.setTag(holder);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.tvWpname.setText(list.get(position).getName());

        if (selectpos == position) {
            if(holder.submenu.getVisibility()==View.VISIBLE)
                holder.submenu.setVisibility(View.GONE);
            else
                holder.submenu.setVisibility(View.VISIBLE);
        } else {
            holder.submenu.setVisibility(View.GONE);
        }

        return convertView;
    }

    public Context getContext() {
        return context;
    }

    public void setContext(Context context) {
        this.context = context;
    }

    public List<Mark> getList() {
        return list;
    }

    public void setList(List<Mark> list) {
        this.list = list;
    }

    @Override
    public Object[] getSections() {
        return new Object[0];
    }

    @Override
    public int getPositionForSection(int sectionIndex) {
        //TODO：数据库接口更改，需要重新编写代码逻辑
//        for (int i = 0; i < getCount(); i++) {
//            String sortStr = list.get(i).getSortLetters();
//            char firstChar = sortStr.toUpperCase().charAt(0);
//            if (firstChar == sectionIndex) {
//                return i;
//            }
//        }

        return -1;
    }

    @Override
    public int getSectionForPosition(int position) {
        return 0;
    }

    protected class ViewHolder {
        private TextView tvWpname;
        private ImageView ivThreeMenuIcon;
        private LinearLayout submenu;
        private LinearLayout mainmenu;

        private Button button;
        private Button button2;
        private Button button3;

        ViewHolder(View view) {
            tvWpname = (TextView) view.findViewById(R.id.tv_wpname);
            ivThreeMenuIcon = (ImageView) view.findViewById(R.id.iv_three_menu_icon);
            submenu = (LinearLayout) view.findViewById(R.id.submenu);
            mainmenu = (LinearLayout) view.findViewById(R.id.mainmenu);
            button = (Button) view.findViewById(R.id.button);
            button2 = (Button) view.findViewById(R.id.button2);
            button3 = (Button) view.findViewById(R.id.button3);
        }
    }

}
